package com.geekstudy.week9.zookeeper;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.util.ArrayList;
import java.util.List;

public class ZookeeperDemo {

    public static void main(String[] args) {

        try {
            String zookeeperUrl = "127.0.0.1:2181";

            Watcher watcher = new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    System.out.println("Watch state:" + watchedEvent.getState() + " type:" + watchedEvent.getType());



                }
            };

            ZooKeeper zooKeeper = new ZooKeeper(zookeeperUrl, 5000, watcher);

            String path1 = "/week9_zookeeper_demo";
            Stat path1Stat = zooKeeper.exists(path1, watcher);
            System.out.println(path1 + " " + path1Stat);

            List<ACL> aclList = ZooDefs.Ids.OPEN_ACL_UNSAFE;
            if(path1Stat == null) {
                String createResult = zooKeeper.create(path1, "小马哥测试week9".getBytes("utf-8"), aclList, CreateMode.PERSISTENT);
                System.out.println(" createResult:" + createResult);
            }

            byte[] byteData = zooKeeper.getData(path1, true, null);
            String stringData = new String(byteData, "utf-8");
            System.out.println(" getData string:" + stringData);

        } catch(Exception e) {
            e.printStackTrace();
        }




    }
}
